package com.aun.hockey.beans;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import org.apache.log4j.Logger;
import com.aun.hockey.server.EmailService;

/**
 * Bean used by the web form.
 * 
 * @see main.xhtml
 * @author ssommer
 */
@RequestScoped
@ManagedBean(name = "form")
public class WebformBean implements Serializable {

	private static final long serialVersionUID = 42L;

	private static Logger log = Logger.getLogger(WebformBean.class);

	private String firstname;
	private String lastname;
	private String email;

	public WebformBean() {
	}

	/* ********** Actions ************************************ */

	/**
	 * Send email with form information.
	 * 
	 * @return
	 */
	public String actionSubmit() {
		try {
			InternetAddress[] recipients = { new InternetAddress("sebastian.sommerfeld@egrima-bc.com") };

			String body = "Hello World from " + firstname + " " + lastname + " (" + email + ")";
			// Additional fields here ...
			// ..........................
			// ..........................

			EmailService service = new EmailService();
			service.sendEmail(recipients, "Test", body);

			return "success";
		} catch (MessagingException e) {
			log.error("Error while sending email", e);
			return "error";
		}
	}

	/* ********** Getter/Setter ****************************** */

	public String getFirstname() {
		return firstname;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	public String getLastname() {
		return lastname;
	}

	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

}
